#!/bin/bash

# databases to backup
databases=("client_honghai") 

# backup directory
basepath='/app/data/backup/mysql/'

if [ ! -d "$basepath" ]; then
  mkdir -p "$basepath"
fi

# loop
for db in ${databases[*]}
  do
    # backup sql
    docker exec  mysql8.0 mysqldump -uroot -p"123456.Valley2021" --databases $db > $basepath$db-$(date +%Y%m%d).sql
    
    # tar sql files
    /bin/nice -n 19 tar zPcf $basepath$db-$(date +%Y%m%d).sql.tar.gz $basepath$db-$(date +%Y%m%d).sql
    
    # delete files 30 days ago
    find $basepath -mtime +30 -name "*.sql.tar.gz" -exec rm -rf {} \;
  done

# delete SQL files
rm -rf $basepath/*.sql
